//
//  TLPlayViewController.m
//  TLAVPlayer
//
//  Created by Tianlong on 2017/5/12.
//  Copyright © 2017年 Tianlong. All rights reserved.
//

#import "TLPlayViewController.h"
#import "TLAVPlayer.h"
#import "Masonry.h"

@interface TLPlayViewController ()<TLPlayerDelegate>
@property (nonatomic , weak) TLAVPlayer *player;
@property (nonatomic , strong) NSMutableArray *dataSource;
@property (nonatomic , strong) NSMutableDictionary *dataSourceDict;
@end

@implementation TLPlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupAVPlayerView];
    
}

-(void)setupAVPlayerView{
    
    TLAVPlayer *player = [[TLAVPlayer alloc] init];
    self.player = player;
    
    self.player.videoUrl = @"https://o79mx21qy.qnssl.com/lmr-cTeyUxoZqNX8nH4_174k_7Px";
//    self.player.videoUrl = @"https://cdn-video-sns.piyingke.com/1fc78e0a-78a0-432c-b8f0-d0807dd9b9ad_-1_-1_.mp4";
    self.player.videoUrl = @"https://mv.xesimg.com/test_library/audio/2018/10/16/t_2590201_10.mp3?1539700678";
    self.player.beginTime = 0;
    self.player.autoPlay = YES;
    self.player.delegate = self;
    self.player.showBackBtnInSmallScreen = NO;
    [self.view addSubview:self.player];
    
    [self.player mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.right.offset(-10);
        make.top.offset(84);
        make.height.offset(200);
    }];
    
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:self.player.videoUrl] options:nil];
    [asset loadValuesAsynchronouslyForKeys:@[@"playable"] completionHandler:^{
        NSError *error = nil;
        AVKeyValueStatus status = [asset statusOfValueForKey:@"playable" error:&error];
        switch (status) {
            case AVKeyValueStatusUnknown:
                NSLog(@" ===================== AVKeyValueStatusUnknown");
                break;
            case AVKeyValueStatusLoaded:
                NSLog(@" ===================== AVKeyValueStatusLoaded");
                break;
            case AVKeyValueStatusFailed:
                NSLog(@" ===================== AVKeyValueStatusFailed");
                break;
            case AVKeyValueStatusCancelled:
                NSLog(@" ===================== AVKeyValueStatusCancelled");
                break;
            default:
                // Handle all other cases
                break;
        }
    }];
}

#pragma mark - TLPlayerDelegate
-(void)player:(TLAVPlayer *)player playerStateDidChanged:(TLPlayerState)state{
    //NSLog(@"播放自当前的状态：state == %lu %s",(unsigned long)state , __FUNCTION__);
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)dealloc{
    [self.player destroy];
}


@end
